package com.hy.doubleCompete.Two131;

import java.util.*;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * User:Mr.Du
 * Date:2024/5/25
 * Time:23:09
 */
public class OccurrencesOfElement {

    public int[] occurrencesOfElement(int[] nums, int[] queries, int x) {
        int n = queries.length;
        int[] res = new int[n];
        Map<Integer, List<Integer>> mp = new HashMap<>();
        for(int i = 0;i < nums.length;i++){
            if(!mp.containsKey(nums[i])){
                List<Integer> list = new ArrayList<>();
                mp.put(nums[i], list);
            }
            mp.get(nums[i]).add(i);
        }
        for(int i = 0;i < n;i++){
            List<Integer> l = mp.get(x);
            if(l == null) res[i] = -1;
            else if(queries[i] > l.size()) res[i] = -1;
            else if(l.get(queries[i] - 1) == null) res[i] = -1;
            else{
                res[i] = l.get(queries[i] - 1);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        OccurrencesOfElement occurrencesOfElement = new OccurrencesOfElement();
        int[] ints = occurrencesOfElement.occurrencesOfElement(new int[]{1, 3, 1, 7}, new int[]{1, 3, 2, 4}, 1);
        System.out.println(Arrays.toString(ints));
    }
}
